<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
   xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
   xmlns:f="http://xmlns.jcp.org/jsf/core"
   xmlns:h="http://xmlns.jcp.org/jsf/html"
   xmlns:p="http://primefaces.org/ui"
   xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
   template="/WEB-INF/templates/default.xhtml">
   
   	<f:metadata>
	
   	</f:metadata>
   	<ui:define name="head">
    	<script type="text/javascript">
    		var websocket; 
    		function createWs() {
    			if (typeof(WebSocket) !== "undefined") {
					var host = window.location.host;
					console.log(host);
					var wsUri = "ws://" + host + "#{request.getContextPath()}/messgaePush/all";
					console.log(wsUri);
					websocket = new WebSocket(wsUri);
			    	
					websocket.onopen = function () {
						console.info('opened');
					};
					websocket.onclose = function () {
						console.info('closed');
						//closeReCreateTimer = window.setTimeout(function() {
							//console.log("closed reconnect");
			            	//createWs();		
						//}, 1000);
					};
			        websocket.onerror = function(evt) { 
			          
			           window.setTimeout(function() {
			            	console.log("error reconnect");
			            	createWs();		
						}, 1000);
			            
			        }; 
					websocket.onmessage = function (evt) {
						console.log(evt.data);
						if (evt.data == 'pong') {
							return false;
						}
						if(evt.data.indexOf("||") != -1){//说明只有特定用户才会收到对应信息
							var arr = evt.data.split("||");
							var message = arr[1];
							var userIds = arr[0].split(",");
							for(var index in userIds){
								var userId = userIds[index];
								if(userId == '2c93f0c260e33b1a0160e419d794002f'){
									//alert("您收到的消息是："+ message);	
								}
							}

						}else{
							//alert("您收到的消息是："+evt.data);	
						}				

					}	
				} else {
					alert("您的浏览器不支持Websocket，请更换浏览器，推荐使用Chrome、Firefox、IE11.0及以上版本。");
				}
    		}

        	$(function(){
        		createWs();
            })
		 
    	</script>
	</ui:define>
   	<ui:define name="content">
   
		<h:form >  
			<ui:remove>
				websocket输入推送消息：<p:inputText value="#{bulletinEndClientEndPointService.message}"></p:inputText><br/>
				<p:commandButton  value="测试消息推送"   action="#{bulletinEndClientEndPointService.pushTestMessage()}" />
			</ui:remove>
				个推推送消息：<p:inputText value="#{bulletinEndClientEndPointService.message}"></p:inputText><br/>
				<p:commandButton  value="测试消息推送"   action="#{bulletinEndClientEndPointService.pushMessage()}" />
			
		</h:form>
      
   	</ui:define>
</ui:composition>
